import axios from 'axios'
import commonParam from './common'

const httpPrefix = commonParam.serverUrl

/**
* 普通post
* @param url
* @param params
* @returns {Promise<any>}
*/
export function post(url, params) {
    return new Promise((resolve, reject) => {
        axios({
            url: httpPrefix + url,
            method: 'post',
            params: params
        }).then(response => {
            resolve(response)
        }).catch(error => {
            reject(error)
        })
    })
}

/**
* post, 参数在requestBody中
* @param url
* @param params
* @returns {Promise<any>}
    */
export function postBody(url, params) {
    return new Promise((resolve, reject) => {
        axios({
            url: httpPrefix + url,
            method: 'post',
            data:params
        }).then(response => {
            resolve(response)
        }).catch(error => {
            reject(error)
        })
    })
}

/**
* get请求
* @param url
* @param params
* @returns {Promise<any>}
*/
export function get(url, params) {
    return new Promise((resolve, reject) => {
        axios({
            url: httpPrefix + url,
            method: 'get',
            params:params
        }).then(response => {
            resolve(response)
        }).catch(error => {
            reject(error)
        })
    })
}

/**
* 通过Post方法
* @param url
* @param param
* @returns {Promise<void>}
*/
export async function synPost(url, params) {
    let res = await axios({
            url: httpPrefix + url,
            method: 'post',
            params: params
        })
    return res;
}

/**
* 获取postbody 的同步数据
* @param url
* @param params
* @returns {Promise<void>}
*/
export async function synPostBody(url, params) {
    let res = await axios({
            url: httpPrefix + url,
            method: 'post',
            data:params
        })
    return res;
}

/**
* 获取get同步数据
* @param url
* @param params
* @returns {Promise<void>}
*/
export async function synGet(url, params) {
    let res = await axios({
            url: httpPrefix + url,
            method: 'get',
            params:params
        })
    return res;
}
